package com.wlcorp.forcazamis.web.action.baseinfo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.baseinfo.IDepotService;
import com.wlcorp.forcazamis.domain.baseinfo.Depot;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class DepotAction extends DispatchAction {
	
	public ActionForward getDepotList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Depot depot = new Depot();
		WebUtil.setRequestToObject(request,depot);
		IDepotService service = ContextUtil.getInstance().getDepotService();
		List<Depot> depotList = service.getDepotList(depot);
		request.setAttribute("depotList",depotList);
		return mapping.findForward("list");
	}
	
	public ActionForward getDepot(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Depot depot = new Depot();
		WebUtil.setRequestToObject(request,depot);
		depot = ContextUtil.getInstance().getDepotService().getDepotById(depot.getDepotId());
		if(null == depot){
			depot = new Depot();
		}
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(null);
		request.setAttribute("staffList",staffList);
		request.setAttribute("depot",depot);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyDepot(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Depot depot = new Depot();
		WebUtil.setRequestToObject(request,depot);
		ContextUtil.getInstance().getDepotService().saveDepot(depot);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == depot.getStatus()){
			WebUtil.setAlertMessage(request,depot.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"操作成功！");
		}
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(null);
		request.setAttribute("staffList",staffList);
		request.setAttribute("depot",depot);
		return mapping.findForward("modify");
	}
	
	public ActionForward getStaffSelectList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		WebUtil.setResponseNoCache(response);
		Staff staff = new Staff();
		WebUtil.setRequestToObject(request,staff);
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(staff);
		if(null != staffList){
			WebUtil.printXML(response,WebUtil.toXMLStr(staffList));
		}
		
		return null;
	}
	
	public ActionForward deleteDepot(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Depot depot = new Depot();
		WebUtil.setRequestToObject(request,depot);
		IDepotService service = ContextUtil.getInstance().getDepotService();
		depot = service.getDepotById(depot.getDepotId());
		if(null != depot){
			service.deleteDepot(depot);
			if(AppConstant.BASE_BEAN_STATUS_ERROR == depot.getStatus()){
				WebUtil.setAlertMessage(request,depot.getMsg());
			}
		}
		return getDepotList(mapping,form,request,response);
	}
}
